_package("alz.mui");

_import("alz.mui.Component");
_import("alz.mui.PaneDebug");

/**
 * 工作区组件
 */
_class("Workspace", Component, function(){
	this._init = function(){
		_super._init.call(this);
		this._modalPanel = null;  //模态蒙板
		this._rootPane = null;    //根面板
		this._tip = null;         //提示框
		this._hideTimer = 0;      //隐藏定时器
		this._delayTimer = 0;     //延迟隐藏定时器
		this._appbox = null;      //应用拖拽的焦点框
	};
	this.create = function(parent, app){
		this.setParent(parent);
		this._app = app;
		var obj = this.createTplElement(parent, "workspace.xml");
		this.init(obj);
		return obj;
	};
	this.init = function(obj){
		_super.init.apply(this, arguments);
		//this.addListener(false);
		if(runtime.getConfData("use_console") && runtime._platform != "iphone"){
			var pane = new PaneDebug();
			pane.create(document.body, this._app);
			runtime.setDebugPane(pane);
		}
	};
	this.dispose = function(){
		this._appbox = null;
		if(this._tip){
			this._tip.dispose();
			this._tip = null;
		}
		if(this._rootPane){
			this._rootPane.dispose();
			this._rootPane = null;
		}
		if(this._modalPanel){
			this._modalPanel.dispose();
			this._modalPanel = null;  //模态对话框用的模态面板
		}
		//this.removeListener(false);
		_super.dispose.apply(this);
	};
	this.getContainer = function(){
		return this._rootPane ? this._rootPane.getContainer() : this._self;
	};
	this.getRootPane = function(){
		return this._rootPane;
	};
	this.setRootPane = function(v){
		this._rootPane = v;
	};
	/**
	 * 通过模板名创建一组DOM元素
	 * @param {Element} parent 父元素
	 * @param {String} tpl 模板名
	 * @return {Element}
	 */
	this.createTplElement = function(parent, tpl, app){
		return this.createDomElement(parent, (app || this._app).getTpl(tpl)/*, ".module"*/);
	};
	this.getModalPanel = function(){
		if(!this._modalPanel){
			this._modalPanel = new ModalPanel();
			this._modalPanel.create(this);
			this._modalPanel.setOpacity(0);
			this._modalPanel.setVisible(false);
		}
		return this._modalPanel;
	};
	this.getTip = function(){
		if(!this._tip){
			this._tip = new TipBox();
			this._tip.create(this);
			this._tip.setVisible(false);
		}
		return this._tip;
	};
	this.showModal = function(component, v){
		var panel = this.getModalPanel();
		if(v){
			panel.pushTarget(component);
			if(component){
				component.setZIndex(runtime.getNextZIndex());
			}
			//this._self.onclick = function(){this._ptr.showModal(false);};
			//panel.setVisible(v);
		}else{
			panel.popTarget();
			//if(!panel.getActiveTarget()){
			//	panel.setVisible(v);
			//}
		}
		if(component){
			component.setVisible(v);
		}
		//this.moveToCenter();
		if(v){
			try{
				if(component){
					component._self.focus();  //设定焦点
				}
			}catch(ex){
			}
			//component.reset(component._req.params);
		}
		panel = null;
	};
	this.showTipMsg = function(msg, model, flag){
		var tip = this.getTip();
		if(!tip) return;
		tip.setFlag(flag || false);  //临时屏蔽加载中提示框
		if(this._hideTimer != 0){
			runtime.stopTimer(this._hideTimer);
			this._hideTimer = 0;
		}
		if(this._delayTimer != 0){
			runtime.stopTimer(this._delayTimer);
			this._delayTimer = 0;
			if(flag){
				this.getModalPanel().popTarget();
			}
		}
		if(tip._msg == msg && tip.getVisible()) return;
		tip.setOpacity(1);
		if(model){
			this.showModal(tip, true);
		}else{
			tip.setVisible(true);
		}
		tip.showMsg(msg);
		this._hideTimer = runtime.startSysTimer(2000, this, function(){
			this.hideTip();
		});
	};
	this.hideTip = function(){
		var tip = this.getTip();
		if(!tip) return;
		//if(!tip.getVisible()) return;
		this._delayTimer = runtime.startSysTimer(250, this, function(){
			tip.setOpacity(0);
			//tip.setVisible(false);
			this.showModal(tip, false);
		});
	};
	this.createRootPane = function(clazz, app){
		var pane = new clazz();
		pane.create(this, app || this._app);
		this.setRootPane(pane);
	};
	this.showAppBox = function(obj, x, y, w, h){
		if(!this._appbox){
			this._appbox = this.createElement(this._self, "div", "wui-AppBox");
			this.createElement(this._appbox, "div");
		}
		this._appbox.style.left = x + "px";
		this._appbox.style.top  = y + "px";
		this._appbox.style.width = w + "px";
		this._appbox.style.height = (h - 2) + "px";
		this._appbox.childNodes[0].style.height = (h - 8) + "px";
		this._appbox.childNodes[0].style.background = "url(" + obj.icon3 + ") no-repeat 100%";
		this._appbox.style.display = "";
	};
	this.hideAppBox = function(){
		if(this._appbox){
			this._appbox.style.display = "none";
		}
	};
});